package cn.com.connext.scrm.service;

import cn.com.connext.scrm.config.Constants;
import cn.com.connext.scrm.domain.master.*;
import cn.com.connext.scrm.repository.master.DwaConvertCodeRepository;
import cn.com.connext.scrm.repository.master.PointManagerRepository;
import cn.com.connext.scrm.repository.master.PrizeExtRepository;
import cn.com.connext.scrm.repository.master.PrizeRepository;
import cn.com.connext.scrm.service.dto.JsonResult;
import cn.com.connext.scrm.service.dto.PointDTO;
import cn.com.connext.scrm.service.dto.PointManagerDTO;
import cn.com.connext.scrm.service.dto.PrizeDTO;
import cn.com.connext.scrm.service.mapper.PointManagerMapper;
import cn.com.connext.scrm.service.mapper.PrizeMapper;
import cn.com.connext.scrm.web.rest.vm.PointManagerPageVM;
import cn.com.connext.scrm.web.rest.vm.PrizeExtVM;
import cn.com.connext.scrm.web.rest.vm.PrizeListVM;
import cn.com.connext.scrm.web.rest.vm.PrizePageVM;
import com.alibaba.fastjson.JSON;
import okhttp3.*;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 2 * @Author: shawn
 * 3 * @Date: 2020/8/11 4:13 下午
 * 4
 */
@Service
public class PointManagerService {
    private final Logger log = LoggerFactory.getLogger(PointManagerService.class);

    @Autowired
    private PointManagerRepository pointManagerRepository;

    @Autowired
    private PointManagerMapper pointManagerMapper;
    @Autowired
    private DwaConvertCodeRepository dwaConvertCodeRepository;


    public PointManager detail(Integer id) {
        return pointManagerRepository.findOne(id);
    }

    public void enable(Integer id) {
    }

    public void delete(Integer id) {
        pointManagerRepository.delete(id);
    }

    public Page<PointManager> list(PointManagerPageVM vm) {
        PageRequest pageRequest = new PageRequest(vm.getPage(), vm.getPageSize(), Sort.Direction.ASC, "id");
        Page<PointManager> page = pointManagerRepository.findAll(new Specification() {
            @Override
            public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder cb) {
                List<Predicate> list = new LinkedList<>();
//                Predicate goodId = cb.equal(root.get("unionId"), vm.getUnionId());
//                list.add(goodId);
                Predicate[] pre = new Predicate[list.size()];
                return query.where(list.toArray(pre)).getRestriction();
            }
        }, pageRequest);
        return page;
    }

    public void edit(PointManager dto) {
        pointManagerRepository.saveAndFlush(dto);
    }

    public void save(PointManager dto) {
        pointManagerRepository.saveAndFlush(dto);
    }

    public List<PointManager> findAll() {
        return pointManagerRepository.findEnable();
    }

    public void addAllDwaCode(List<DwaConvertCode> dwaList) {
        dwaConvertCodeRepository.save(dwaList);
    }
}
